package com.ajaxdemo.web; /**
 * @author wzm
 * @create 2023-05-17 16:37
 */

import com.ajaxdemo.entity.Employee;
import com.ajaxdemo.service.EmployeeService;
import com.ajaxdemo.service.impl.EmployeeServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/employee/*")
public class EmployeeServlet extends BaseServlet {
    private EmployeeService employeeService = new EmployeeServiceImpl();

    //查询全部的方法
    protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用Service查询全部数据
        List<Employee> byAll = employeeService.findByAll();//java对象
        //java->字符串
        String s = JSON.toJSONString(byAll);
        //将结果影响给客户端
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(s);
    }


    //新增
    protected void addEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取数据
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        //转为对象
        Employee employee = JSON.parseObject(s, Employee.class);
        System.out.println("employee = " + employee);
        //调用service方法
        response.getWriter().write(""+employeeService.add(employee));



    }
}
